<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['insurance:employer:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['insurance:employer:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['insurance:employer:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['insurance:employer:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="employerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="联系人" align="center" prop="contact" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="通讯地址" align="center" prop="mailAddress" />
      <el-table-column label="邮编" align="center" prop="emailCode" />
      <el-table-column label="邮箱号码" align="center" prop="email" />
      <el-table-column label="保险人性质" align="center" prop="nature" :formatter="natureFormat" />
      <el-table-column label="所在行业" align="center" prop="industry" />
      <el-table-column label="保险人地址" align="center" prop="address" />
      <el-table-column label="员工总数" align="center" prop="staffCount" />
      <el-table-column label="高级情况说明" align="center" prop="seniorDescription" />
      <el-table-column label="普通情况说明" align="center" prop="generalDescription" />
      <el-table-column label="用户类型" align="center" prop="type"  :formatter="typeFormat"/>
      <el-table-column width="200" label="邮箱号码" align="center" prop="email" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['insurance:employer:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['insurance:employer:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用工单位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="通讯地址" prop="mailAddress">
          <el-input v-model="form.mailAddress" placeholder="请输入通讯地址" />
        </el-form-item>
        <el-form-item label="邮编" prop="emailCode">
          <el-input v-model="form.emailCode" placeholder="请输入邮编" />
        </el-form-item>
        <el-form-item label="邮箱号码" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱号码" />
        </el-form-item>
        <el-form-item label="保险人地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入保险人地址" />
        </el-form-item>
        <el-form-item label="保险人营业范围" prop="businessScope">
          <el-input v-model="form.businessScope" placeholder="请输入保险人营业范围址" />
        </el-form-item>
        <el-form-item label="所在行业" prop="industry">
          <el-input v-model="form.industry" placeholder="请输入所在行业" />
        </el-form-item>
        <el-form-item label="保险人性质" prop="nature">
          <el-radio-group v-model="form.nature">
            <el-radio
              v-for="dict in natureOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="保险员工总数" prop="staffCount">
          <el-input v-model="form.staffCount" placeholder="请输入保险员工总数" />
        </el-form-item>
        <el-form-item label="高级情况说明" prop="seniorDescription">
          <el-input type="textarea"
                    :rows="5" v-model="form.seniorDescription" placeholder="请输入高级职员姓名，职务，健康情况说明" />
        </el-form-item>
        <el-form-item label="普通情况说明" prop="generalDescription">
          <el-input type="textarea"
                    :rows="5" v-model="form.generalDescription" placeholder="请输入普通职员姓名，职务，健康情况说明" />
        </el-form-item>
        <el-form-item label="用户类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listEmployer, getEmployer, delEmployer, addEmployer, updateEmployer, exportEmployer } from "@/api/insurance/employer";

  export default {
    name: "Employer",
    components: {
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用工单位表格数据
        employerList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 删除标志字典
        delFlagOptions: [],
        statusOptions:[],//投保性质
        typeOptions:[],//用户类型
        natureOptions:[],//保险人性质
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          sysUid: null,
          name: null,
          contact: null,
          phone: null,
          mailAddress: null,
          emailCode: null,
          email: null,
          delFlag: null
        },
        // 表单参数
        form: {
          type: "1"
        },
        // 表单校验
        rules: {
          name: [
            { required: true, message: "名称不能为空", trigger: "blur" }
          ],
          contact: [
            { required: true, message: "联系人不能为空", trigger: "blur" }
          ],
          phone: [
            { required: true, message: "电话不能为空", trigger: "blur" },
            {pattern: /^1[3|4|5|7|8][0-9]{9}$/, message: '请输入正确的联系人电话'}
          ],
          mailAddress:[
            { required: true, message: "通讯地址不能为空", trigger: "blur" }
          ],
          emailCode:[
            { required: true, message: "邮编不能为空", trigger: "blur" }
          ],
          email:[
            { required: false, message: '邮箱号码', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱号码', trigger: ['blur', 'change'] }
          ],
          address:[
            { required: true, message: '保险人地址不能为空', trigger: 'blur' },
          ],
          businessScope:[
            { required: true, message: '保险人营业范围不能为空', trigger: 'blur' },
          ],
          industry:[
            { required: true, message: '所在行业不能为空', trigger: 'blur' },
          ],
          nature:[
            { required: true, message: '保险人性质不能为空', trigger: 'blur' },
          ],
          type:[
            { required: true, message: '用户类型不能为空', trigger: 'blur' },
          ]

        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_del_flag").then(response => {
        this.delFlagOptions = response.data;
      });
      this.getDicts("insure_nature").then(response => {
        this.natureOptions = response.data;
      });
      this.getDicts("employer_type").then(response => {
        this.typeOptions = response.data;
      });
    },
    methods: {
      /** 查询用工单位列表 */
      getList() {
        this.loading = true;
        // console.log('queryParams',this.queryParams)
        listEmployer(this.queryParams).then(response => {
          console.log('查询所有数据',response)
          this.employerList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 状态字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.type);
      },
      natureFormat(row, column) {
        return this.selectDictLabel(this.natureOptions, row.nature);
      },
      // 删除标志字典翻译
      delFlagFormat(row, column) {
        return this.selectDictLabel(this.delFlagOptions, row.delFlag);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          sysUid: null,
          name: null,//名称
          contact: null,//联系人
          phone: null,//联系人电话
          mailAddress: null,//通讯地址
          emailCode: null,//邮编
          email: null,//邮箱号码
          delFlag: null,
          address:null,//被保险人地址
          businessScope:null,//被保险人营业范围
          generalDescription:null,//普通职员姓名，职务，健康情况说明
          industry:null,//所在行业
          nature:'3',//被保险人性质
          seniorDescription:'',//高级职员姓名，职务，健康说明
          staffCount:null,//被保险人总数
          type:'1'
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加用工单位";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getEmployer(id).then(response => {
          this.form = response.data;
          this.form.type=this.form.type.toString()
          console.log('response',response)
          this.open = true;
          this.title = "修改用工单位";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateEmployer(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addEmployer(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除用工单位编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEmployer(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有用工单位数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportEmployer(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>

